package services

import (
	"TaskAssignmentSystem/internal/app/dao"
	"TaskAssignmentSystem/pkg/utils"
	"errors"
	"fmt"
	"strconv"
)

type SystemDictService struct {
}

func (s *SystemDictService) GetUserAgreement() (result string, err error) {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.CustomWhere = append(systemDictDao.CustomWhere, fmt.Sprintf("dict_code = 'ZXZ_USER_AGREEMENT'"))
	res, err := systemDictDao.First(map[string]interface{}{})
	if err != nil {
		return "", errors.New("用户协议不存在")
	}
	return res.DictValue, nil
}
func (s *SystemDictService) GetPrivacyAgreement() (result string, err error) {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.CustomWhere = append(systemDictDao.CustomWhere, fmt.Sprintf("dict_code = 'ZXZ_PRIVACY_AGREEMENT'"))
	res, err := systemDictDao.First(map[string]interface{}{})
	if err != nil {
		return "", errors.New("隐私协议不存在")
	}
	return res.DictValue, nil
}

func (s *SystemDictService) GetDictByCode(code string) (res dao.SystemDict, err error) {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.CustomWhere = append(systemDictDao.CustomWhere, fmt.Sprintf("dict_code = '%s'", code))
	res, err = systemDictDao.First(map[string]interface{}{})
	if err != nil {
		return res, errors.New("数据不存在")
	}
	return res, nil
}

func (s *SystemDictService) GetDictById(id uint) (res dao.SystemDict, err error) {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.CustomWhere = append(systemDictDao.CustomWhere, fmt.Sprintf("id = %d", id))
	res, err = systemDictDao.First(map[string]interface{}{})
	if err != nil {
		return res, errors.New("数据不存在")
	}
	return res, nil
}

func (s *SystemDictService) GetValueByCode(code string) (result string, err error) {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.CustomWhere = append(systemDictDao.CustomWhere, fmt.Sprintf("dict_code = '%s'", code))
	res, err := systemDictDao.First(map[string]interface{}{})
	if err != nil {
		return "", errors.New("数据不存在")
	}
	return res.DictValue, nil
}

func (s *SystemDictService) GetValueByType(dtype string) (result []dao.SystemDict, err error) {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.Query = map[string]interface{}{"dict_type": dtype}
	res, err := systemDictDao.FindAll(systemDictDao.Query)
	if err != nil {
		return result, errors.New("数据不存在")
	}
	return res, nil
}

func (s *SystemDictService) GetValueByTypeV2(dictTpye, dictValue, description, dictCode string) (result []dao.SystemDict, err error) {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.Query = map[string]interface{}{"dict_type": dictTpye}
	if utils.StringIsNotEmpty(dictCode) {
		systemDictDao.Query["dict_code"] = dictCode
	}
	if utils.StringIsNotEmpty(description) {
		systemDictDao.Query["description"] = description
	}
	if utils.StringIsNotEmpty(dictValue) {
		systemDictDao.Query["dict_value"] = dictValue
	}
	res, err := systemDictDao.FindAll(systemDictDao.Query)
	if err != nil {
		return result, errors.New("数据不存在")
	}
	return res, nil
}

func (s *SystemDictService) GetValueByTypeV3(dictTpye string, dictCode []string) (result []dao.SystemDict, err error) {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.Query = map[string]interface{}{"dict_type": dictTpye}
	systemDictDao.Query["dict_code"] = dictCode
	res, err := systemDictDao.FindAll(systemDictDao.Query)
	if err != nil {
		return result, errors.New("数据不存在")
	}
	return res, nil
}

func (s *SystemDictService) UpdateValueAndNumber(id, number uint, val string) error {
	var systemDictDao dao.Common[dao.SystemDict]
	set := map[string]interface{}{"dict_number": number}
	if val != "" {
		set["dict_value"] = val
	}
	return systemDictDao.UpdateOne(id, set).Error
}

func (s *SystemDictService) DictTypeInsertOrUpdateNumber(dictTpye, dictValue string, dictNumber int) error {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.Query = map[string]interface{}{"dict_type": dictTpye, "dict_value": dictValue}
	if res, err := systemDictDao.First(systemDictDao.Query); err != nil {
		var record dao.SystemDict
		record.DictCode = "ZXZ_" + dictValue + "级"
		record.DictType = dictTpye
		record.DictValue = dictValue
		record.DictNumber = dictNumber
		record.Description = "团队等级有效人数（value等级，num数量）"
		return systemDictDao.CreateOne(record).Error
	} else {
		set := map[string]interface{}{"dict_number": dictNumber}
		return systemDictDao.UpdateOne(res.Id, set).Error
	}
}

func (s *SystemDictService) DictTypeInsertOrUpdateNumberV2(dictTpye, dictValue, description, dictCode string) error {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.Query = map[string]interface{}{"dict_type": dictTpye, "dict_code": dictCode}
	if res, err := systemDictDao.First(systemDictDao.Query); err != nil {
		var record dao.SystemDict
		record.DictCode = dictCode       // 1
		record.DictType = dictTpye       // ZXZ_PHONE_CODE
		record.DictValue = dictValue     // 134
		record.Description = description // 中国移动
		return systemDictDao.CreateOne(record).Error
	} else {
		set := map[string]interface{}{"dict_value": dictValue, "description": description}
		return systemDictDao.UpdateOne(res.Id, set).Error
	}
}

func (s *SystemDictService) UpdateValueAndDescription(id uint, val, description string) error {
	var systemDictDao dao.Common[dao.SystemDict]
	set := map[string]interface{}{"dict_value": val, "description": description}
	return systemDictDao.UpdateOne(id, set).Error
}

func (s *SystemDictService) GetUserNumber() (result string, err error) {
	var systemDictDao dao.Common[dao.SystemDict]
	systemDictDao.CustomWhere = append(systemDictDao.CustomWhere, fmt.Sprintf("dict_code = 'ZXZ_USER_NUMBER'"))
	res, err := systemDictDao.First(map[string]interface{}{})
	if err != nil {
		return "", errors.New("数据不存在")
	}
	systemDictDao.Query = map[string]interface{}{"dict_code": "ZXZ_USER_NUMBER"}
	systemDictDao.Update(map[string]interface{}{"dict_value": s.getUserNumberNextValue(res.DictValue)})
	return res.DictValue, nil
}

func (s *SystemDictService) getUserNumberNextValue(oldValue string) (result string) {
	a, _ := strconv.Atoi(oldValue)
	newValue := fmt.Sprintf("%08d", a+1)
	return newValue
}
